//
//  CycleBaseViewController.m
//  CycleBaseControl
//
//  Created by ddy on 2018/10/19.
//  Copyright © 2018年 cycle. All rights reserved.
//

#import "CycleBaseViewController.h"
#import <objc/runtime.h>
#import <objc/message.h>
@interface CycleBaseViewController ()

@end
//方法二:消息(需要在点击项目,收索msg改为NO,此时可以打出Objc_msgSend()),self表示发送者,SEL(setTabBar:)表示发送什么,tabBar这里表示具体内容,属于底层
//    objc_msgSend(self, @selector(setTabBar:),tabBar);

///替换类的方法
//void cycle_exchangeInstanceMethod(Class class,SEL originalSelector,SEL newSelector) {
//    //传入class类存在的方法
//    Method originalMethod = class_getInstanceMethod(class, originalSelector);
//    //在class中传入想要替换class的originalMethod的方法
//    Method newMethod = class_getInstanceMethod(class, newSelector);
//    if (class_addMethod(class, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
//        class_replaceMethod(class, newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
//    } else {
//        method_exchangeImplementations(originalMethod, newMethod);
//    }
//
//
//}
@implementation CycleBaseViewController

//调用alloc会默认调用allocWithZone方法
//+ (instancetype)allocWithZone:(struct _NSZone *)zone {
//    CycleBaseViewController *viewController = [super allocWithZone:zone];
//    //加载视图
//    cycle_exchangeInstanceMethod([self class], @selector(viewDidLoad), @selector(cycle_viewDidLoad));
//    //视图将要显示
//    cycle_exchangeInstanceMethod([self class], @selector(viewWillAppear:), @selector(cycle_viewWillAppear:));
//    //视图将要消失
//    cycle_exchangeInstanceMethod([self class], @selector(viewWillDisappear:), @selector(cycle_viewWillDisappear:));
//
//    return viewController;
//}
//- (void)cycle_viewDidLoad {
//    CycleBaseViewController *viewController = self;
//    [viewController cycle_setNavgation];
//    [viewController cycle_addSubViews];
//    [viewController cycle_getNewDataFromViewDidLoad];
//}
//- (void)cycle_viewWillAppear:(BOOL)animated {
//    CycleBaseViewController *viewController = self;
//    [viewController cycle_getNewDataFromViewWillAppear];
//}
//- (void)cycle_viewWillDisappear:(BOOL)animated {
//    CycleBaseViewController *viewController = self;
//    [viewController cycle_viewWillDisAppear];
//}





#pragma mark --- 系统回调函数(暂时这样使用)
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self cycle_setNavgation];
    [self cycle_addSubViews];
    [self cycle_getNewDataFromViewDidLoad];
    
    //键盘问题
    [self cycle_dealKeyBoard];
    
}
//视图将要显示
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self cycle_getNewDataFromViewWillAppear];
}
//视图完全显示
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self cycle_getNewDataFromViewDidAppear];
}
//视图将要消失
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self cycle_viewWillDisAppear];
}
//视图完全消失
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self cycle_viewDidDisAppear];
}

#pragma mark --- <CycleBaseViewControllerProtocol>
/**
 * 添加控件
 */
- (void)cycle_addSubViews{}
/**
 * 设置导航栏
 */
- (void)cycle_setNavgation{}
/**
 * 在viewDidLoad中获取数据
 */
- (void)cycle_getNewDataFromViewDidLoad{}
/**
 * 在viewWillAppear中获取数据
 */
- (void)cycle_getNewDataFromViewWillAppear{}
/**
 * 在viewdidAppear中获取数据
 */
- (void)cycle_getNewDataFromViewDidAppear{}
/**
 * 在视图将要消失的时候做处理
 */
- (void)cycle_viewWillDisAppear{}
/**
 * 在视图已经消失中做处理
 */
- (void)cycle_viewDidDisAppear{}


#pragma mark --- 处理键盘问题
- (void)cycle_dealKeyBoard {
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cycle_keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGes.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGes];
    
    
   //键盘将要弹起
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cycle_keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   //键盘将要消失
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cycle_keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
/**
 * 当用户点击其他控件,并没有点击self.view的时候也退下键盘
 */
- (void)cycle_keyboardHide:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
/**
 * 键盘将要弹起
 */
- (void)cycle_keyBoardWillShow:(NSNotification *)note {
    
    
    //弹起的时候防止键盘遮住了文本输入框
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    //获取当前的控件firstResponder
    UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
    //判断该控件是否是文本框
    if ([firstResponder isKindOfClass:[UITextField class]] || [firstResponder isKindOfClass:[UITextView class]]) {
        //获取键盘高度
        //使用UIKeyboardFrameBeginUserInfoKey,会出现切换输入法时获取的搜狗键盘不对.
        CGFloat keyBoardHeight = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
        CGRect frame = firstResponder.frame;
        
            CGFloat offSet = frame.origin.y + frame.size.height - (self.view.frame.size.height - keyBoardHeight); //iphone键盘高度为216.iped键盘高度为352
        
            [UIView beginAnimations:@"ResizeForKeyboardKey" context:nil];
        
            [UIView setAnimationDuration:0.5f];
        
            //将试图的Y坐标向上移动offset个单位，以使线面腾出开的地方用于软键盘的显示
        
            if (offSet > 0) {
            
                self.view.frame = CGRectMake(0.0f, -offSet, self.view.frame.size.width, self.view.frame.size.height);
            
                    [UIView commitAnimations];
           }
        
        
//        return;
//        //是文本框,将文本框坐标系转换到self.view上
//        CGRect rect = [firstResponder convertRect:firstResponder.frame toView:self.view];
//        //获取键盘高度
//        //使用UIKeyboardFrameBeginUserInfoKey,会出现切换输入法时获取的搜狗键盘不对.
//        CGFloat keyBoardHeight = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
//        //偏移量
//        CGFloat offset = rect.origin.y + rect.size.height + keyBoardHeight - self.view.bounds.size.height;
//
//        if (offset > 0) {
//            self.view.transform = CGAffineTransformMakeTranslation(0, -offset);
//        }
    }
    
    
    
}
/**
 * 键盘将要消失
 */
- (void)cycle_keyBoardWillHide:(NSNotification *)note {
    //还原位置
//    self.view.transform = CGAffineTransformIdentity;
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}





/*
 // 将像素point由point所在视图转换到目标视图view中，返回在目标视图view中的像素值
 - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
 // 将像素point从view中转换到当前视图中，返回在当前视图中的像素值
 - (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
 
 // 将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
 // 将rect从view中转换到当前视图中，返回在当前视图中的rect
 - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
 
 例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
 
 // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell，cell上放有一个button
 // 在controllerA中实现:
 CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
 或
 CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
 // 此rc为btn在controllerA中的rect
 
 或当已知btn时：
 CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
 或
 CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
 */

/*
 break : 跳出当前函数某个循环体,执行循环体 后面的内容或者循环体
 continue : 当满足某个条件continue时,并不会跳出该函数的这个循环体,而是跳过这个条件,继续循环
 return : 是跳出整个函数,满足条件后,即使后面有再多函数和循环体都不会在执行
 */
@end
